package com.qicheng.wlhyspider.common;

import cn.hutool.http.HttpResponse;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

import java.time.Duration;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

@Slf4j
public class HttpUtils {
    public static HttpResponse httpGet(String url,RedisUtils redisUtils) {
        LocalDateTime start = LocalDateTime.now();
        HttpResponse httpResponse = HttpUtil.createGet(url)
                .header("x-auth-token", redisUtils.get(Constants.TOKEN) + "")
                .execute();
        String token = httpResponse.header("X-Auth-Token");
        LocalDateTime end = LocalDateTime.now();
        Duration duration = Duration.between(start,end);
        log.info("调用接口耗时：{}秒",duration.getSeconds());
        if (StringUtils.isNotBlank(token)){
            redisUtils.set(Constants.TOKEN,token);
        }
        return httpResponse;
    }

    public static HttpResponse TjHttpGet(String url,RedisUtils redisUtils) {
        LocalDateTime start = LocalDateTime.now();
        HttpResponse httpResponse = HttpUtil.createGet(url)
                .header("X-Access-Token", redisUtils.get(Constants.TJ_TOKEN) + "")
                .execute();
        Map<String, List<String>> headers = httpResponse.headers();
        log.info("====headers=={}", JSONObject.toJSONString(headers));
        String token = httpResponse.header("X-Access-Token");
        LocalDateTime end = LocalDateTime.now();
        Duration duration = Duration.between(start,end);
        log.info("调用接口耗时：{}秒",duration.getSeconds());
        if (StringUtils.isNotBlank(token)){
            redisUtils.set(Constants.TJ_TOKEN,token);
        }
        return httpResponse;
    }

    public static HttpResponse GzHttpPost(String url,RedisUtils redisUtils,JSONObject body) {
        LocalDateTime start = LocalDateTime.now();
        HttpResponse httpResponse = HttpUtil.createPost(url)
                .header("Token", redisUtils.get(Constants.GZ_TOKEN) + "")
                .header("account", "52106456")
                .body(body.toJSONString())
                .execute();
        Map<String, List<String>> headers = httpResponse.headers();
        log.info("====headers=={}", JSONObject.toJSONString(headers));
        String token = httpResponse.header("Token");
        LocalDateTime end = LocalDateTime.now();
        Duration duration = Duration.between(start,end);
        log.info("调用接口耗时：{}秒",duration.getSeconds());
        if (StringUtils.isNotBlank(token)){
            redisUtils.set(Constants.GZ_TOKEN,token);
        }
        return httpResponse;
    }

    public static HttpResponse ahHttpGet(String url,RedisUtils redisUtils) {
        LocalDateTime start = LocalDateTime.now();
        HttpResponse httpResponse = HttpUtil.createGet(url)
                .header("x-auth-token", redisUtils.get(Constants.AH_TOKEN) + "")
                .execute();
        String token = httpResponse.header("X-Auth-Token");
        LocalDateTime end = LocalDateTime.now();
        Duration duration = Duration.between(start,end);
        log.info("调用接口耗时：{}秒",duration.getSeconds());
        if (StringUtils.isNotBlank(token)){
            redisUtils.set(Constants.AH_TOKEN,token);
        }
        return httpResponse;
    }
}
